﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using UnitTestModel;

namespace Widgetsphere.AcmeUnitTests
{
	[TestFixture]
	public class TestInsert
	{
		[Test]
		public void TestSelectAndUpdate()
		{
			try
			{
				using (UnitTestEntities context = new UnitTestEntities())
				{
					var simpleTableEntries = from simp in context.SimpleTable
													 select simp;
					foreach (SimpleTable current in simpleTableEntries)
					{
						Console.WriteLine(current.Name);
						current.Name = "R";
					}

					context.SaveChanges();


					foreach (SimpleTable current in simpleTableEntries)
					{
						Console.WriteLine(current.Name);
						current.Name = "P";
					}
					context.SaveChanges();

                    var alltypeEntries = from allTypes in context.AllTypes select allTypes;
                    foreach (AllTypes current in alltypeEntries)
                    {
                        Console.WriteLine(current.Nvarchar);
                        current.Nvarchar = "New NVarChar";
                    }
                    context.SaveChanges();

				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
				TestSelectAndUpdate();
			}
		}

        [Test]
        public void TestSelectAndDelete()
        {
            try
            {
                using (UnitTestEntities context = new UnitTestEntities())
                {
                    var simpleTableEntries = from simp in context.SimpleTable
                                             select simp;

                    foreach (SimpleTable current in simpleTableEntries)
                    {
                        Console.WriteLine(current.Name);
                        context.DeleteObject(current);
                    }
                    context.SaveChanges();

                    var alltypeEntries = from allTypes in context.AllTypes select allTypes;
                    foreach (AllTypes current in alltypeEntries)
                    {
                        Console.WriteLine(current.Nvarchar);
                        context.DeleteObject(current);
                    }
                    context.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                TestSelectAndDelete();
            }
        }


		[Test]
		public void TestInsertSimpleEnity()
		{
			try
			{
				using (UnitTestEntities context = new UnitTestEntities())
				{

					SimpleTable simpleTable = new SimpleTable();
					simpleTable.SimpleTableGuid = Guid.NewGuid();
					simpleTable.Name = "Name 2";
					context.AddToSimpleTable(simpleTable);
					context.SaveChanges();

					IdentityTest identity = new IdentityTest();
					identity.Name = "Name 1";
					context.AddToIdentityTest(identity);

					IdentityTest identity2 = new IdentityTest();
					identity2.Name = "Name 1";
					context.AddToIdentityTest(identity2);
					context.SaveChanges();

					AllTypes allTypes = new AllTypes();
					allTypes.AllTypeId = Guid.NewGuid();
					allTypes.Bigint = 1;
					allTypes.Binary = new byte[] { };
					allTypes.Bit = true;
					allTypes.CharTest = "CharTest";
					allTypes.DateTest = "Fred";
					allTypes.Datetime = DateTime.Now;
					//allTypes.Datetime2 = "string";
					//allTypes.Datetimeoffset = "string";
					allTypes.DecimalTest = new decimal(3.2);
					allTypes.FloatTest = 1.0;
					allTypes.Image = new byte[] { };
					allTypes.IntTest = 1;
					allTypes.Money = new decimal(1.25);
					allTypes.Nchar = "NChar";
					allTypes.Ntext = "NText";
					allTypes.Numeric = new decimal(2.34);
					allTypes.Nvarchar = "Nvarchar";
					allTypes.Nvarcharmax = "Nvarcharmax";
					allTypes.Real = (float)2.3;
					allTypes.Smalldatetime = DateTime.Now;
					allTypes.Smallint = 1;
					allTypes.Smallmoney = new decimal(3.45);
					allTypes.SqlVariant = "SqlVariant";
					allTypes.Text = "Text";
					//allTypes.Time = "Time";
					allTypes.Tinyint = (byte)2;
					allTypes.Uniqueidentifier = Guid.NewGuid();
					allTypes.Varbinary = new byte[] { };
					allTypes.Varbinarymax = new byte[] { };
					allTypes.Varchar = "VarChar";
					allTypes.Varcharmax = "VarCharMax";
					allTypes.Xml = "<test/>";
					context.AddToAllTypes(allTypes);
					context.SaveChanges();



				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}
	}
}
